package com.tenbit.emp.ctl;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.tenbit.emp.svc.EMP1000SVC;
import com.tenbit.emp.vo.EMP1000VO;

import emp.tenbit.emp.bean.SearchEmpBean;
import emp.tenbit.emp.bean.TestBean;

@Controller
public class EMP1000CTL {
	@Autowired
	private EMP1000SVC emp1000svc;
	private Logger log=Logger.getLogger(this.getClass());
	
	@RequestMapping("/doSearchEmpView.do")
	public ModelAndView doSearchEmpView(@RequestParam(value="searchName",required=false) String name){
		ModelAndView mv = new ModelAndView();
		
		if(name != null){
			mv.addObject("EMP",emp1000svc.getEmpSearchList(name));
			
		}
		mv.setViewName("EMP1030P0");
		
		return mv;
		
	}
	@RequestMapping("/test.do")
	public String test(TestBean bean,HttpServletRequest req){
		
		/*Iterator<Entry<String, String[]>> it=req.getParameterMap().entrySet().iterator();
		
		while(it.hasNext()){
			Entry<String, String[]> entry=it.next();
			
			log.debug("test "+entry.getKey());
			
			for (String s : entry.getValue()) {
				log.debug(s);
			}
			
		}*/
		
		Enumeration<String> e=req.getParameterNames();
		while (e.hasMoreElements()) {
			log.debug("test1 "+e.nextElement());
		}
		
		return "test2";
	}
	public ModelAndView doInitMain(){
		ModelAndView mv=new ModelAndView();
		List<EMP1000VO> sex_code=new ArrayList<EMP1000VO>();
		List<EMP1000VO> dept_code=new ArrayList<EMP1000VO>();
		List<EMP1000VO> skill_code=new ArrayList<EMP1000VO>();
		List<EMP1000VO> academic_code=new ArrayList<EMP1000VO>();
		
		mv.setViewName("EMP1000M0");
		mv.addObject("listZip", emp1000svc.getCodeZipByMvList());
		
		for (EMP1000VO zip : emp1000svc.getCodeZipByMvList()) {
			if(zip.getCode_type().equals("SEX")){
				sex_code.add(zip);
			}else if(zip.getCode_type().equals("DEPT")){
				dept_code.add(zip);
			}if(zip.getCode_type().equals("SKILL")){
				skill_code.add(zip);
			}else if(zip.getCode_type().equals("ACADEMIC")){
				academic_code.add(zip);
			}
		}
		
		mv.addObject("sex_code", sex_code);
		mv.addObject("dept_code", dept_code);
		mv.addObject("skill_code", skill_code);
		mv.addObject("academic_code", academic_code);
		
		
		return mv;
	}
	@RequestMapping("/empMainView.do")
	public ModelAndView doViewMain(){
		return doInitMain();
	}
	@RequestMapping("/empInsertView.do")
	public ModelAndView doViewIns(){
		ModelAndView mv=doInitMain();
		
		mv.setViewName("EMP1010P0");
		
		return mv;
	}
	@RequestMapping("/listEmp.do")
	public ModelAndView getEmpList(SearchEmpBean searchBean,@RequestParam(value="page",required=false) String page){
		log.debug("==========================>getEmpList "+searchBean);
		ModelAndView mv=doInitMain();
		
		if(searchBean.getName()!= null){	
			mv.addObject("viewBean", emp1000svc.doMakeView(searchBean));
		}
		mv.setViewName("EMP1000M0");
		mv.addObject("viewTable","yes");
		
		return emp1000svc.getEmpList(searchBean,page,mv);
	}
	@RequestMapping("/empDelOrModView.do")
	public ModelAndView doViewDelOrMod(@RequestParam("emp_id") String empId){
		
		ModelAndView mv=doInitMain();
		
		emp1000svc.getDelOrModList(mv,empId);
		mv.setViewName("EMP1020P0");
		mv.addObject("employee_id", empId);
		
		return mv;
	}
}
